package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.Result;
import com.pro_one.pojo.OrderSetting;
import com.pro_one.service.OrderSettingService;
import com.pro_one.util.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置信息功能
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * excel文件上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            List<String[]> readExcel = POIUtils.readExcel(multipartFile);
            if (readExcel != null && readExcel.size() > 0) {
                List<OrderSetting> orderSettings = new ArrayList<>();
                for (String[] rows : readExcel) {
                    String orderDate = rows[0];
                    String number = rows[1];
                    orderSettings.add(new OrderSetting(new Date(orderDate), Integer.parseInt(number)));
                    System.out.println(orderDate);
                    System.out.println(new Date(orderDate));
                }
                orderSettingService.add(orderSettings);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据月份查询所有预约设置信息展示到页面
     *
     * @param date
     * @return
     */
    @RequestMapping("/findOrdersettingByMonth")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result findOrdersettingByMonth(String date) {
        try {
            List<Map> map = orderSettingService.findOrdersettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 设置可预约人数信息
     *
     * @param orderSetting
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result edit(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.edit(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);

    }
}
